import { IHostsContentObject, IHostsListObject } from '@root/common/data'
import events from '@root/common/events'
import { broadcast } from '@main/core/agent'
import { findItemById } from '@root/common/hostsFn'
import { Request, Response } from 'express'
import {
    getBasicData,
    getHostsContent,
    getList,
    setList,
    setHostsContent,
  } from '@root/main/actions'
  import { swhdb } from '@root/main/data'

const addList = async (req: Request, res: Response) => {

    let configItemId = req.query.id
    let configItemContext = req.query.context

    console.log(`http_api configItem: ${configItemId}-${configItemContext}`)
    if (!configItemId) {
      res.end('bad configItem.')
      return
    }
    // setHostsContent('temphost','12.12.12.12 test.com')
    // let infoContext = await getHostsContent('temphost')

    getHostsContent('temphost').then(result=>{
        console.log('rowContent:' + result);
        if(result != null && result != ''){
            setHostsContent('temphost',result+"\n"+configItemContext).then(r=>{
                broadcast(events.hosts_content_changed, 'temphost')
            })
        }else{
            let h: IHostsListObject = {
                id: 'temphost',
                "title":"IT平台HOST配置",
                "content":'',
                "on":true
            }
        
            swhdb.list.tree.push(h).then(v=>{
                setHostsContent('temphost',configItemContext+"").then(r=>{
                    broadcast(events.hosts_content_changed, 'temphost')
                })
            });        
        }
    })



    res.end('success')
  }
  
  export default addList